3 * Copyright © 2011 Sam Reed
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\MediaWikiServices
;
24 use MediaWiki\Revision\RevisionAccessException
;
25 use MediaWiki\Revision\RevisionRecord
;
26 use MediaWiki\Revision\RevisionStore
;
27 use MediaWiki\Revision\SlotRecord
;
32 class ApiFeedContributions
extends ApiBase
{
34 /** @var RevisionStore */
35 private $revisionStore;
38 * This module uses a custom feed wrapper printer.
40 * @return ApiFormatFeedWrapper
42 public function getCustomPrinter() {
43 return new ApiFormatFeedWrapper( $this->getMain() );
46 public function execute() {
47 $this->revisionStore
= MediaWikiServices
::getInstance()->getRevisionStore();
49 $params = $this->extractRequestParams();
51 $config = $this->getConfig();
52 if ( !$config->get( 'Feed' ) ) {
53 $this->dieWithError( 'feed-unavailable' );
56 $feedClasses = $config->get( 'FeedClasses' );
57 if ( !isset( $feedClasses[$params['feedformat']] ) ) {
58 $this->dieWithError( 'feed-invalid' );
61 if ( $params['showsizediff'] && $this->getConfig()->get( 'MiserMode' ) ) {
62 $this->dieWithError( 'apierror-sizediffdisabled' );
65 $msg = wfMessage( 'Contributions' )->inContentLanguage()->text();
66 $feedTitle = $config->get( 'Sitename' ) . ' - ' . $msg .
67 ' [' . $config->get( 'LanguageCode' ) . ']';
68 $feedUrl = SpecialPage
::getTitleFor( 'Contributions', $params['user'] )->getFullURL();
70 $target = $params['user'] == 'newbies'
72 : Title
::makeTitleSafe( NS_USER
, $params['user'] )->getText();
74 $feed = new $feedClasses[$params['feedformat']] (
76 htmlspecialchars( $msg ),
80 // Convert year/month parameters to end parameter
81 $params['start'] = '';
83 $params = ContribsPager
::processDateFilter( $params );
85 $pager = new ContribsPager( $this->getContext(), [
87 'namespace' => $params['namespace'],
88 'start' => $params['start'],
89 'end' => $params['end'],
90 'tagFilter' => $params['tagfilter'],
91 'deletedOnly' => $params['deletedonly'],
92 'topOnly' => $params['toponly'],
93 'newOnly' => $params['newonly'],
94 'hideMinor' => $params['hideminor'],
95 'showSizeDiff' => $params['showsizediff'],
98 $feedLimit = $this->getConfig()->get( 'FeedLimit' );
99 if ( $pager->getLimit() > $feedLimit ) {
100 $pager->setLimit( $feedLimit );
104 if ( $pager->getNumRows() > 0 ) {
106 $limit = $pager->getLimit();
107 foreach ( $pager->mResult
as $row ) {
108 // ContribsPager selects one more row for navigation, skip that row
109 if ( ++
$count > $limit ) {
112 $item = $this->feedItem( $row );
113 if ( $item !== null ) {
114 $feedItems[] = $item;
119 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
122 protected function feedItem( $row ) {
123 // This hook is the api contributions equivalent to the
124 // ContributionsLineEnding hook. Hook implementers may cancel
125 // the hook to signal the user is not allowed to read this item.
127 $hookResult = Hooks
::run(
128 'ApiFeedContributions::feedItem',
129 [ $row, $this->getContext(), &$feedItem ]
131 // Hook returned a valid feed item
132 if ( $feedItem instanceof FeedItem
) {
134 // Hook was canceled and did not return a valid feed item
135 } elseif ( !$hookResult ) {
139 // Hook completed and did not return a valid feed item
140 $title = Title
::makeTitle( (int)$row->page_namespace
, $row->page_title
);
141 if ( $title && $title->userCan( 'read', $this->getUser() ) ) {
142 $date = $row->rev_timestamp
;
143 $comments = $title->getTalkPage()->getFullURL();
144 $revision = $this->revisionStore
->newRevisionFromRow( $row );
147 $title->getPrefixedText(),
148 $this->feedItemDesc( $revision ),
149 $title->getFullURL( [ 'diff' => $revision->getId() ] ),
151 $this->feedItemAuthor( $revision ),
160 * @since 1.32, takes a RevisionRecord instead of a Revision
161 * @param RevisionRecord $revision
164 protected function feedItemAuthor( RevisionRecord
$revision ) {
165 $user = $revision->getUser();
166 return $user ?
$user->getName() : '';
170 * @since 1.32, takes a RevisionRecord instead of a Revision
171 * @param RevisionRecord $revision
174 protected function feedItemDesc( RevisionRecord
$revision ) {
175 $msg = wfMessage( 'colon-separator' )->inContentLanguage()->text();
177 $content = $revision->getContent( SlotRecord
::MAIN
);
178 } catch ( RevisionAccessException
$e ) {
182 if ( $content instanceof TextContent
) {
183 // only textual content has a "source view".
184 $html = nl2br( htmlspecialchars( $content->getText() ) );
186 // XXX: we could get an HTML representation of the content via getParserOutput, but that may
187 // contain JS magic and generally may not be suitable for inclusion in a feed.
188 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
189 // Compare also FeedUtils::formatDiffRow.
193 $comment = $revision->getComment();
195 return '<p>' . htmlspecialchars( $this->feedItemAuthor( $revision ) ) . $msg .
196 htmlspecialchars( FeedItem
::stripComment( $comment ?
$comment->text
: '' ) ) .
197 "</p>\n<hr />\n<div>" . $html . '</div>';
200 public function getAllowedParams() {
201 $feedFormatNames = array_keys( $this->getConfig()->get( 'FeedClasses' ) );
205 ApiBase
::PARAM_DFLT
=> 'rss',
206 ApiBase
::PARAM_TYPE
=> $feedFormatNames
209 ApiBase
::PARAM_TYPE
=> 'user',
210 ApiBase
::PARAM_REQUIRED
=> true,
213 ApiBase
::PARAM_TYPE
=> 'namespace'
216 ApiBase
::PARAM_TYPE
=> 'integer'
219 ApiBase
::PARAM_TYPE
=> 'integer'
222 ApiBase
::PARAM_ISMULTI
=> true,
223 ApiBase
::PARAM_TYPE
=> array_values( ChangeTags
::listDefinedTags() ),
224 ApiBase
::PARAM_DFLT
=> '',
226 'deletedonly' => false,
229 'hideminor' => false,
231 ApiBase
::PARAM_DFLT
=> false,
235 if ( $this->getConfig()->get( 'MiserMode' ) ) {
236 $ret['showsizediff'][ApiBase
::PARAM_HELP_MSG
] = 'api-help-param-disabled-in-miser-mode';
242 protected function getExamplesMessages() {
244 'action=feedcontributions&user=Example'
245 => 'apihelp-feedcontributions-example-simple',